package cn.znzz.cjm.org.controller;

import cn.znzz.cjm.basic.util.AjaxResult;
import cn.znzz.cjm.basic.util.PageList;
import cn.znzz.cjm.org.domain.Dictionarytype;
import cn.znzz.cjm.org.query.DictionarytypeQuery;
import cn.znzz.cjm.org.service.IDictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门接口
 */
@RestController
@RequestMapping("/dict")
public class DictionarytypeController {
    @Autowired
    private IDictionarytypeService dictionarytypeService;

    //新增或修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if (dictionarytype.getId() == null){
                dictionarytypeService.add(dictionarytype);
            }else {
                dictionarytypeService.update(dictionarytype);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试！");
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionarytypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试！");
        }
    }
    //批量删除  前段传的是数字，可以用list和数组接收
    @PatchMapping
    public AjaxResult BatchRemove(@RequestBody List<Long> ids){
        try {
            dictionarytypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试！");
        }
    }

    //根据id查询
    @GetMapping("/{id}")
    public Dictionarytype queryById(@PathVariable("id") Long id){
        return dictionarytypeService.findById(id);
    }

    //查询所有
    @GetMapping
    public List<Dictionarytype> queryAll(){
        return dictionarytypeService.findAll();
    }

    //分页查询 高级查询
    @PostMapping("/list")
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery query){
        return dictionarytypeService.queryPage(query);
    }
}